
// <copyright file="DbTransactionContext.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   This class wraps-up a db transaction.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Data;

    /// <summary>
    ///     This class wraps-up a db transaction.
    /// </summary>
    public sealed class DbTransactionContext : AbstractDisposable
    {
        #region Fields

        /// <summary>
        ///     The _transaction.
        /// </summary>
        private IDbTransaction _transaction;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DbTransactionContext"/> class.
        /// </summary>
        /// <param name="connection">
        /// The connection.
        /// </param>
        public DbTransactionContext(IDbConnection connection)
        {
            this.Connection = connection;
            this._transaction = connection.BeginTransaction();
            this.IsCommitted = false;
        }

        /// <summary>
        ///     Finalizes an instance of the <see cref="DbTransactionContext" /> class.
        /// </summary>
        ~DbTransactionContext()
        {
            this.Dispose(false);
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the connection.
        /// </summary>
        public IDbConnection Connection { get; private set; }

        /// <summary>
        /// Gets a value indicating whether [is committed].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is committed]; otherwise, <c>false</c>.
        /// </value>
        public bool IsCommitted { get; private set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// Creates the new.
        /// </summary>
        /// <returns>
        /// DbTransactionContext.
        /// </returns>
        public static DbTransactionContext CreateNew()
        {
            IDbConnection connection = DbConnectionFactory.Instance.CreateConnection();
            return new DbTransactionContext(connection);
        }

        /// <summary>
        ///     Commits this instance.
        /// </summary>
        public void Commit()
        {
            this.CheckAndThrowDisposedException();
            this._transaction.Commit();
            this.IsCommitted = true;
        }

        /// <summary>
        ///     Creates the command.
        /// </summary>
        /// <returns>IDbCommand.</returns>
        public IDbCommand CreateCommand()
        {
            this.CheckAndThrowDisposedException();
            IDbCommand command = this.Connection.CreateCommand();
            command.Transaction = this._transaction;
            return command;
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <param name="disposing">
        /// if set to <c>true</c> [disposing].
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (!this.IsCommitted)
            {
                this._transaction.Rollback();
            }

            this._transaction.Dispose();
            this.Connection.Close();
            this._transaction = null;
            this.Connection = null;
        }

        #endregion Methods
    }
}